package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.AddTipsActivity;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Task;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class PhotoListAdapter extends BaseAdapter
    implements ObservableAdapter    {
    private Group<Task> mTasks;
    private int mLayoutToInflate;
    private LayoutInflater mInflater;
    private Context mContext;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();
    private ViewHolder mViewHolder;

    public PhotoListAdapter(Context context, RemoteResourceManager rrm, Group<Task> tasks)   {
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.share_photo_list_item;
        mTasks = tasks;
        mContext = context;
        mRrm = rrm;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mRrm.addObserver(mResourcesObserver);
    }

    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhotos);
        mRrm.deleteObserver(mResourcesObserver);
    }

    public int getCount()   {
        return mTasks.size();
    }

    public Task getItem(int position) {
        return mTasks.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)   {

        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);
            mViewHolder = new ViewHolder();
            mViewHolder.username = (TextView)convertView.findViewById(R.id.tvUserName);
            mViewHolder.hideImage = (ImageView)convertView.findViewById(R.id.imgHideImage);
            mViewHolder.photo = (ImageView)convertView.findViewById(R.id.imgPhoto);
            mViewHolder.hideImage.setTag(View.GONE);
            mViewHolder.date = (TextView)convertView.findViewById(R.id.tvPhotoDate);
            mViewHolder.address = (TextView)convertView.findViewById(R.id.tvPhotoAddress);
            convertView.setTag(mViewHolder);
        }  else  {
            mViewHolder = (ViewHolder)convertView.getTag();
        }

        Task task = mTasks.get(position);
        mViewHolder.username.setText(task.getUserName());
        Uri photoUri = Uri.parse(task.getPhoto());
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
            mViewHolder.photo.setImageBitmap(bitmap);
        } catch (IOException e) {
            mViewHolder.photo.setImageResource(R.drawable.blank_girl);
        }
        mViewHolder.photo.setTag(position);


        mViewHolder.photo.setTag(new Integer(position));
        mViewHolder.photo.setOnClickListener(mOnClickListenerPhoto);
        mViewHolder.hideImage.setOnClickListener(mOnClickListenerHide);
        mViewHolder.date.setText(task.getPostDate());
        mViewHolder.address.setText(task.getVenueName());
        return convertView;
    }

    OnClickListener mOnClickListenerHide = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int mPhotoStatus = (Integer)mViewHolder.photo.getTag();
            if(mPhotoStatus == View.GONE){
                mViewHolder.photo.setVisibility(View.VISIBLE);
            }else if(mPhotoStatus == View.VISIBLE){
                mViewHolder.photo.setVisibility(View.GONE);
            }

        }
    };

    OnClickListener mOnClickListenerPhoto = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position = (Integer) v.getTag();
            Intent intent = new Intent(mContext, AddTipsActivity.class);
            intent.putExtra(AddTipsActivity.EXTRA_TASK_PARCEL,
                    mTasks.get(position));
            mContext.startActivity(intent);
        }
    };

    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhotos);
        }
    }

    private Runnable mUpdatePhotos = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    static class ViewHolder {
        TextView username;
        ImageView hideImage;
        ImageView photo;
        TextView date;
        TextView address;
    }

}
